package com.hospital.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.hospital.dao.EquipmentDao;
import com.hospital.dto.EquipmentDto;
import com.hospital.dto.EquipmentTjDto;
import com.hospital.entity.Equipment;
import com.hospital.service.intf.EquipmentService;
import com.hospital.util.StringUtil;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentDao dao;
    @Override
    public R add(Equipment equipment) {
     if (dao.add(equipment)>0){
         return R.ok();
     }else {
         return R.fail();
     }
    }

    @Override
    public R deleteById(int id) {
        if (dao.deleteById(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(Equipment equipment) {
       if (dao.update(equipment)>0){
           return R.ok();
       }else {
           return R.fail();
       }
    }

    @Override
    public PageBean selectPage(String name, int page, int limit) {
        if (StringUtil.isNotEmpty(name)) {
            name = "%" + name + "%";
        }
            int start =(page-1)*limit;
        return PageBean.ok(dao.selectCount(name),dao.selectPage(name,start,limit));
    }

    @Override
    public List<Equipment> all() {
        return  dao.all();
    }

    @Override
    public R deleByIds(int[] ids) {
      if (dao.delectByIds(ids)>0){
          return R.ok();
      }else {
          return R.fail();
      }
    }

    @Override
    public R save(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            //2.解析Excel -EasyExcel
            //2.解析Excel数据
            List<Equipment> list= EasyExcel.read(file.getInputStream(), Equipment.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {}
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
            }).sheet().doReadSync();
            //3.Mybatis 实现批量新增
            if(dao.save(list)>0){
                //4.返回
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R onditionTj() {
      List<EquipmentDto> list = dao.seleOndition();
      List<EquipmentTjDto> result = new ArrayList<>();

      list.forEach(s->{
          result.add(new EquipmentTjDto(s.getOndition(),s.getNum()));
      });
      return R.ok(result);
    }
//    private String parseOndition(String ondition){
//        String r = "";
//        switch(ondition){
//            case r ="良好";break;
//            case r ="待维护";break;
//            default:r="其他状况";break;
//        }
//        return r;
//    }
}
